<?php
//管理文章栏目管理，对应article_catalog表
FLEA::loadClass('Common_Model');

class Model_ArticleCatalog extends Common_Model {

	private $_ui = null;
	
	function __construct() {
		parent::Common_Model();
		$this->_tb = FLEA::getSingleton('Table_ArticleCatalog');
	}

	//返回只含有栏目名的组：默认为array(catalogname = catalogid),若$retType='id2name'则返回array(catalogid = catalogname)
	function getAllCatalogs($retType = null) {
		$row = $this->_tb->findAll();
		foreach ($row as $data) {
			$viewData['catalogname'][] = $data['catalog_name'];
			$viewData['catalogid'][] = $data['catalog_id'];
		}
		if ($retType == 'id2name') {
			$viewData = array_combine($viewData['catalogid'],$viewData['catalogname']);	//php的数组操作真让人无语....
		}else {
			$viewData = array_combine($viewData['catalogname'],$viewData['catalogid']);
		}
		dump($row);
		dump($viewData);
		
		return $viewData;
	}
	
	//render所有栏目的已生成的HTML代码
	function renderAllCatalogs() {
		$viewData['data'] = $this->renderList($this->_tb->findAll());
		return $viewData;
	}
	
	function getCatalogById($catalogId) {
		return $this->_tb->findByField ('catalog_id', $catalogId);
	}

	function insertCatalog($row) {
		$this->existsRecord('catalog_name', $row['catalog_name'], '该栏目已存在，请更换名称！', url('articleCatalog', 'insertCatalog'));
		return $this->_tb->create($row);
	}
	
	function updateCatalog($row) {
		$this->existsRecord('catalog_name', $row['catalog_name'], '该栏目已存在，请更换名称！', url('articleCatalog'));
		return $this->_tb->update($row);
	}
	
	function deleteCatalogById($catalogId) {
		return $this->_tb->removeByPkv($catalogId);
	}
	
	function deleteAllCatalogs() {
		//forbidden
	}
	
	//用PEAR的HTML_Table类生成表格，返回的是表格的HTML代码
	function renderList($listData) {
		//require_once 'HTML/Table.php';
		$tableFacade = FLEA::getSingleton('Common_TableFacade');
		//set table attributes
		$attributes = array("width"=>"100%", "border"=>"0");

		//create the table class
		$table = new $tableFacade($attributes);
		
		$table->setCaption('栏目列表&nbsp;&nbsp;&nbsp;&nbsp;<a href=' . url('articleCatalog', 'insertCatalog') . '>添加新栏目</a>');
		//$table->setRowCount(10);
		
		//build our first row
		$contents = array("ID", "NAME", "OPERATION", "CREATED");
		$attributes = array('bgcolor'=>"#336699");
		$table->addRow($contents, $attributes, "TH");
		
		//loop through and add our data
		$attributes = array('bgcolor'=>"#COCOCO");

		foreach ($listData as $data) {
			$strOperation = '<a href=\'' . url ( 'ArticleCatalog', 'modifyCatalog', array ('catalog' => $data ['catalog_id'] ) ) . '\'>修改</a>&nbsp;&nbsp;<a href=\'' . url ( 'ArticleCatalog', 'deleteCatalog', array ('catalog' => $data ['catalog_id'] ) ) . '\'>删除</a>';
			$contents = array($data['catalog_id'], $data['catalog_name'], $strOperation, $data['created']);
			$table->addRow($contents, $attributes);
		}

		return $table->toHTML();
	}
	
	//生成角色添加和修改的表单元素
	function getForm($data) {
		$this->_ui = FLEA::initWebControls();
		$form = array(
			'catalog_id' => $this->_ui->control('Hidden', 'catalog_id',
				array(
					'value' => $data['catalog_id']
				),
				TRUE 
		),
			'catalog_name' => $this->_ui->control('Textbox', 'catalog_name',
				array(
					'value' => $data['catalog_name'],
					'maxlength' => 18
				),
				TRUE
			)
		);
		return $form;
	}

}
